package com.nmgs.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.nmgs.dto.CommonTextDto;
import com.nmgs.dto.ScreenDto;
import com.nmgs.dto.SpecialEventDto;
import com.nmgs.dto.SpecialEventDto;
import com.nmgs.entity.SetupTable;
import com.nmgs.mapper.SpecialEventMapper;
import com.nmgs.service.ISpecialEventService;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SpecialEventServiceImpl implements ISpecialEventService {

  public SpecialEventMapper specialEventMapper;
    @Autowired
    public void setSpecialEventMapper(SpecialEventMapper specialEventMapper) {
        this.specialEventMapper = specialEventMapper;
    }

    @Override
    public SpecialEventDto getLaneData(Map<String, Object> params) {
        SpecialEventDto screenDto=new SpecialEventDto();
        try{
            List<SpecialEventDto> setupTableList = specialEventMapper.getSetupTableList(params);
            if(!setupTableList.isEmpty()){
                BigDecimal totalFlow=BigDecimal.ZERO;
                BigDecimal inFlow=BigDecimal.ZERO;
                BigDecimal outFlow=BigDecimal.ZERO;
                int error=0;
                for(SpecialEventDto setupTable:setupTableList){
                    totalFlow=totalFlow.add(setupTable.getTotalFlow());
                    if("OUT".equals(setupTable.getSpecialType())){
                        outFlow=outFlow.add(setupTable.getTotalFlow());
                    }
                    if("IN".equals(setupTable.getSpecialType())){
                        inFlow=inFlow.add(setupTable.getTotalFlow());
                    }
                }
                screenDto.setTotalFlow(totalFlow);
                screenDto.setInportCount(inFlow);
                screenDto.setOutportCount(outFlow);
            }
        }catch (Exception e){
            screenDto.setTotalFlow(BigDecimal.ZERO);
            screenDto.setInportCount(BigDecimal.ZERO);
            screenDto.setOutportCount(BigDecimal.ZERO);
            return screenDto;
        }
        return screenDto;
    }
    @Override
    public List<SpecialEventDto> getSpecialList(Map<String, Object> params) {
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE", PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return specialEventMapper.getSpecialList(params);
    }

    @Override
    public List<SpecialEventDto> getSpecialSolveList(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<SpecialEventDto> flowAndMoneyByEnname2 =null;
        if("DAY".equals(dateType)){
            String[] hours24 = new String[24];
            // 使用循环填充数组
            for(int i = 0; i < 24; i++) {
                //hours24[i] = String.format("%02d", i); // 保持两位数格式
                hours24[i] = i+""; // 保持两位数格式
            }
            flowAndMoneyByEnname2=specialEventMapper.getSpecialDayList(params);
            List<SpecialEventDto> list=new ArrayList<SpecialEventDto>();
            for(String str:hours24){
                SpecialEventDto SpecialEventDtoTemp=new SpecialEventDto();
                BigDecimal totalFlow=new BigDecimal("0");
                BigDecimal vncCount=BigDecimal.ZERO;
                BigDecimal speakCount=BigDecimal.ZERO;
                BigDecimal operatorCount=BigDecimal.ZERO;
                if(!flowAndMoneyByEnname2.isEmpty()){
                    for (SpecialEventDto SpecialEventDto : flowAndMoneyByEnname2) {
                        if(str.equals(SpecialEventDto.getDmDate())){
                            SpecialEventDtoTemp.setVncCount(SpecialEventDto.getVncCount());
                            SpecialEventDtoTemp.setSpeakCount(SpecialEventDto.getSpeakCount());
                            SpecialEventDtoTemp.setOperatorCount(SpecialEventDto.getOperatorCount());
                        }
                    }
                    if(str.length()<2){
                        str="0"+str;
                    }
                    SpecialEventDtoTemp.setDmDate(str);

                    list.add(SpecialEventDtoTemp);
                }else{
                    if(str.length()<2){
                        str="0"+str;
                    }
                    SpecialEventDtoTemp.setDmDate(str);
                    SpecialEventDtoTemp.setVncCount(vncCount);
                    SpecialEventDtoTemp.setSpeakCount(speakCount);
                    SpecialEventDtoTemp.setOperatorCount(operatorCount);
                    list.add(SpecialEventDtoTemp);
                }
            }
            flowAndMoneyByEnname2=list;
        }else{
            flowAndMoneyByEnname2=specialEventMapper.getSpecialWMList(params);
            if(!flowAndMoneyByEnname2.isEmpty()){
                for (SpecialEventDto SpecialEventDto : flowAndMoneyByEnname2) {
                    SpecialEventDto.setDmDate(PubTools.getDateMonthDay(SpecialEventDto.getClassdate()));
                }
            }
        }
        return flowAndMoneyByEnname2;
    }

    @Override
    public List<SpecialEventDto> getSpecialListStat(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return specialEventMapper.getSpecialListStat(params);
    }
    @Override
    public List<SpecialEventDto> getSpecialReason(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return specialEventMapper.getSpecialReason(params);
    }
    @Override
    public List<SpecialEventDto> getSpecialSolveStat(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        return specialEventMapper.getSpecialSolveStat(params);
    }
    @Override
    public List<SpecialEventDto> getSpecialRank(Map<String, Object> params){
        String dateType = params.get("dateType").toString();
        params.put("DBTYPE",PubTools.getDBType());
        if("DAY".equals(dateType)){
            params.put("startTime",PubTools.getdfYMD());
            params.put("endTime",PubTools.getdfYMD());
        }else if("WEEK".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(6));
            params.put("endTime",PubTools.getDecNumDate(0));
        }else if("MONTH".equals(dateType)){
            params.put("startTime",PubTools.getDecNumDate(29));
            params.put("endTime",PubTools.getDecNumDate(0));
        }
        List<SpecialEventDto> specialTotal = specialEventMapper.getSpecialTotal(params);
        List<SpecialEventDto> specialListAll = new ArrayList<>();
        Map<Integer,SpecialEventDto> map=new HashMap<Integer, SpecialEventDto>();
        if(!specialTotal.isEmpty()){
            for (SpecialEventDto specialEventDto : specialTotal) {
                String binaryString = Long.toBinaryString(specialEventDto.getFlag());//转换为二进制字符串
                if (!"0".equals(binaryString)) {
                    char[] bytes = binaryString.toCharArray();
                    int j = 1;
                    for (int k = bytes.length - 1; k > -1; k--) {
                        if (bytes[k] == '1') {
                          if(map.containsKey(j)){
                              SpecialEventDto specialEventDtoTemp= map.get(j);
                              specialEventDtoTemp.setFlagCount(specialEventDtoTemp.getFlagCount()+(specialEventDto.getFlagCount()));
                              specialEventDtoTemp.setDt(specialEventDto.getDt().before(specialEventDtoTemp.getDt())?specialEventDtoTemp.getDt():specialEventDto.getDt());
                              map.put(j,specialEventDtoTemp);
                          }else{
                              map.put(j,specialEventDto);
                          }
                        }
                        j++;
                    }
                }
            }
        }
/*     Map<String, SpecialEventDto> sortedByValue = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(
                        Comparator.comparingDouble(SpecialEventDto::getFlagCount)
                )).collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new
                ));*/

        List<Map.Entry<Integer, SpecialEventDto>> collect = map.entrySet().stream()
                .sorted(Comparator.comparing(e -> e.getValue().getFlagCount()))
                .collect(Collectors.toList());
        for(int k=collect.size()-1;k>-1;k--){
            SpecialEventDto value = collect.get(k).getValue();
            Integer key = collect.get(k).getKey();;
            Map<String,Object> mapT=new HashMap<String, Object>();
            mapT.put("value2",key);
            List<CommonTextDto> flagText = specialEventMapper.getFlagText(mapT);
            if(!flagText.isEmpty()){
                value.setSpecialName(flagText.get(0).getText2());
                specialListAll.add(value);
            }
        }
/*        for (Map.Entry<Integer, SpecialEventDto> entry : map.entrySet()) {
            Integer key = entry.getKey();
            Map<String,Object> mapT=new HashMap<String, Object>();
            mapT.put("value2",key);
            SpecialEventDto value = entry.getValue();
            List<CommonTextDto> flagText = specialEventMapper.getFlagText(mapT);
            if(!flagText.isEmpty()){
                value.setSpecialName(flagText.get(0).getText2());
                specialListAll.add(value);
            }
        }*/
        return specialListAll;
    }
}
